//
//  STDatePickerView.m
//
//  Created by yls on 14-2-25.
//  Copyright (c) 2014年 yls. All rights reserved.
//
// http://git.oschina.net/yanglishuan/STDatePickerView
//

#import "STDatePickerView.h"

@interface STDatePickerView()
{
    NSMutableArray *onShowFinishBlocks;
    NSMutableArray *onHideFinishBlocks;
    NSMutableArray *onDoneBlocks;
    NSMutableArray *onCancelBlocks;
    NSMutableArray *onDateChangedBlocks;
    
    void (^ onShowFinish)(STDatePickerView * pickerView);
    void (^ onHideFinish)(STDatePickerView * pickerView);
    
    void (^ onDone)(STDatePickerView * pickerView);
    void (^ onCancel)(STDatePickerView * pickerView);
    void (^ onDateChanged)(STDatePickerView * pickerView, NSDate *date);
}

@end

@implementation STDatePickerView

- (id)initWithFrame:(CGRect)frame
{
    CGFloat width = [[UIScreen mainScreen] bounds].size.width;
    self = [super initWithFrame:CGRectMake(0, 0, width, 260)];
    if (self) {
        onShowFinishBlocks = [NSMutableArray array];
        onHideFinishBlocks = [NSMutableArray array];
        onDoneBlocks = [NSMutableArray array];
        onCancelBlocks = [NSMutableArray array];
        onDateChangedBlocks = [NSMutableArray array];
        
        UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, width, 216)];
        _datePicker = datePicker;
        [datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:datePicker];
        
        UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, width, 44)];
        
        if (([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)) {
            toolBar.barStyle = UIBarStyleDefault;
        } else {
            toolBar.barStyle = UIBarStyleDefault;
        }
        
        toolBar.translucent = YES;
        
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];
        cancelItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel:)];
        UIBarButtonItem *middleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        middleItem.width = width - 120;
        UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)];
        doneItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(done:)];
        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        
        toolBar.items = @[leftItem, cancelItem, middleItem, doneItem, rightItem];
        
        [self addSubview:toolBar];
    }
    return self;
}

- (id)init
{
    self = [self initWithFrame:CGRectZero];
    if (self) {
        
    }
    return self;
}

- (void)setDate:(NSDate *)date
{
    _datePicker.date = date;
}

- (NSDate *)date
{
    return _datePicker.date;
}

- (void)cancel:(id)sender
{
    if (onCancel) {
        onCancel(self);
    }
    [self dismiss:YES];
}

- (void)done:(id)sender
{
    if(onDone) {
        onDone(self);
    }
    [self dismiss:YES];
}

- (void)datePickerValueChanged:(UIDatePicker *)datePicker
{
    if (onDateChanged) {
        onDateChanged(self, datePicker.date);
    }
}

- (void)dismiss:(BOOL)animated;
{
    if (!_isShow) {
        return;
    }
    
    _isShow = NO;
    
    if (animated) {
        [UIView beginAnimations:@"PickerHide" context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDuration:.25];
        [UIView setAnimationDelegate:self];
        CGRect frame = self.frame;
        frame.origin.y = self.superview.frame.size.height;
        self.frame = frame;
        [UIView setAnimationDidStopSelector:@selector(st_animationDidStopHide)];
        [UIView commitAnimations];
    } else {
        CGRect frame = self.frame;
        frame.origin.y = self.superview.frame.size.height;
        self.frame = frame;
    }
}

- (void)show:(BOOL)animated;
{
    if (_isShow) {
        return;
    }
    
    _isShow = YES;
    
    CGRect frame = self.frame;
    frame.origin.y = self.superview.frame.size.height;
    self.frame = frame;
    
    frame.origin.y = self.superview.frame.size.height - self.frame.size.height;

    if (animated) {
        [UIView beginAnimations:@"PickerShow" context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDuration:.25];
        [UIView setAnimationDelegate:self];
        self.frame = frame;
        [UIView setAnimationDidStopSelector:@selector(st_animationDidStopShow)];
        [UIView commitAnimations];
    } else {
        self.frame = frame;
    }
}

- (void)st_animationDidStopShow
{
    if (onShowFinish) {
        onShowFinish(self);
    }
}

- (void)st_animationDidStopHide
{
    if (onHideFinish) {
        onHideFinish(self);
    }
}

- (void)showInView:(UIView *)view animated:(BOOL)animated;
{
    if (!self.superview) {
        CGRect frame = self.frame;
        frame.origin.y = view.frame.size.height;
        self.frame = frame;
        
        [view addSubview:self];
    }
    
    [self show:animated];
}

+ (STDatePickerView *)showInView:(UIView *)view animated:(BOOL)animated;
{
    return [self showInView:view date:nil animated:animated];
}

+ (STDatePickerView *)showInView:(UIView *)view date:(NSDate *)date animated:(BOOL)animated;
{
    STDatePickerView *datePickerView = [[STDatePickerView alloc] initWithFrame:CGRectZero];
    [datePickerView showInView:view animated:animated];
    if (date) {
        datePickerView.date = date;
    }
    return datePickerView;
}

/**显示完成事件 */
- (void)onShowFinish:(void(^)(STDatePickerView *pickerView))showFinish;
{
    onShowFinish = showFinish;
}

/**隐藏完成事件 */
- (void)onHideFinish:(void(^)(STDatePickerView *pickerView))hideFinish;
{
    onHideFinish = hideFinish;
}

- (void)onDone:(void (^)(STDatePickerView *))done
{
    onDone = done;
}

- (void)onCancel:(void (^)(STDatePickerView *))cancel
{
    onCancel = cancel;
}

- (void)onDateChanged:(void (^)(STDatePickerView *, NSDate *))dateChangedBlock
{
    onDateChanged = dateChangedBlock;
}

@end
